package com.example.asus.a20180917.recyclerview;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.example.asus.a20180917.R;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        //定义适配器对象
        RecyclerViewAdapter Adapter = new RecyclerViewAdapter();
        //获取数据
        Adapter.setDate(getData());
        //需将布局中的RecyclerView对象的id指定为recyclerview
        RecyclerView recyclerView = findViewById(R.id.RecyclerView);
        //定义线性方式显示数据  布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager LayoutManager = new LinearLayoutManager(this);
        //设置为位垂直方式显示
        LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);


        //提示框
        RecyclerViewAdapter.OnItemClickListener onItemClickListener = new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //System.out.println("dawangba:");
                Toast.makeText(RecyclerViewActivity.this,"200 OK"+getData().get(position).getText1(), Toast.LENGTH_SHORT).show();
            }
        };
        Adapter.setOnItemClickListener(onItemClickListener);


        //设置布局管理器
        recyclerView.setLayoutManager(LayoutManager);
        //设置适配器
        recyclerView.setAdapter(Adapter);

        //添加分割线
        //添加 Android内部提供的 默认水平分割线
        //recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        //添加自己设计的分割线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(this,R.drawable.fengexian));
        recyclerView.addItemDecoration(dividerItemDecoration);
    }

    //获取数据集合
    private List<Dataitem> getData() {
        List<Dataitem> data = new ArrayList<>();

        for(int i=0;i<50;i++)
        {
            Dataitem dataItem = new Dataitem();

            dataItem.setUrl("image"+i);
            dataItem.setText1("Text1："+i);
            dataItem.setText2("Text2："+i);

            data.add(dataItem);
        }
        return data;
    }}
